ParacolExtJSMessageFilters = function(extension) {

    function isComponent(value) {
        return value.hasOwnProperty("otype");
    }

    function createAndDecorateComponent(value, ctx) {
        var component = createComponent(value, ctx);
        component.messageValue = value;
        return component;
    }

    function containsItem(arr, item) {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] == item) {
                return true;
            }
        }
        return false;
    }

    /**
     * Extension point
     * @param value
     */
    function createComponent(value, ctx) {
        var otype = value.otype;

        var config;

        var localizedLabel;
        if (value.CommonMetaData && value.CommonMetaData.title) {
            localizedLabel = value.CommonMetaData.title
        }

        if (value.ListItems) {
            if (value.ListItems.multiChoice) {
                // MyDesktop.MultiChoiceField.Store
                var msStore = Ext.create('MyDesktop.MultiChoiceField.Store', {
                    data : value.ListItems.data
                });
//                var msStore = new Ext.data.ArrayStore({
//                    idIndex: 0,
//                    fields: ['value', 'title', 'description'],
//                    data : value.ListItems.data
//                });

                if (value.value) {
                    value.value = value.value.join(","); // array -> csv string
                } else {
                    value.value = "";
                }
                config = {
                    id: value.id,
                    fieldLabel: localizedLabel || value.label,
                    name: value.name,
                    value: value.value,
                    datastore: msStore
                };
                //return new Ext.chloroform.MultiChoiceField(config);
                return Ext.create('MyDesktop.MultiChoiceField', config);


            } else {
                var store = new Ext.data.ArrayStore({
                    fields: ['value', 'title', 'description'],
                    data : value.ListItems.data
                });

                var comboConfig = {
                    id: value.id,
                    name: value.name,
                    value: value.value,
                    valueField: 'value',
                    displayField: 'title',
                    editable: value.ListItems.editable,
                    tpl: '<tpl for="."><div ext:qtip="{title}({value}). {description}" class="x-combo-list-item">{title}</div></tpl>',
                    fieldLabel: localizedLabel || value.label,
                    store: store,
                    typeAhead: false,
                    mode: 'local',
                    triggerAction: 'all',
                    emptyText:'Select a value...',
                    selectOnFocus:false
                };

                if (value.RefInfo) {
                    comboConfig.trigger2Cls = Ext.baseCSSPrefix + 'form-arrow-trigger';
                    comboConfig.onTrigger2Click = function() {
                        alert("trigger2"); // todo
                    };

//                    var combo = new Ext.form.ComboBox({
//                        trigger2Cls: Ext.baseCSSPrefix + 'form-arrow-trigger',
//                        id: value.id,
//                        name: value.name,
//                        value: value.value,
//                        valueField: 'value',
//                        displayField: 'title',
//                        editable: value.ListItems.editable,
//                        tpl: '<tpl for="."><div ext:qtip="{title}({value}). {description}" class="x-combo-list-item">{title}</div></tpl>',
//                        fieldLabel: localizedLabel || value.label,
//                        store: store,
//                        typeAhead: false,
//                        mode: 'local',
//                        triggerAction: 'all',
//                        emptyText:'Select a value...',
//                        selectOnFocus:false,
//
//                        onTrigger2Click: function() {
//                            alert("trigger2");
//                        }
//                    });
                }
                return new Ext.form.ComboBox(comboConfig);
            }
        } else {
            switch (otype) {
                // fields
                case "DateField":
                    config = {
                        id: value.id,
                        fieldLabel: localizedLabel || value.label,
                        name: value.name,
                        value: value.value
                    };
                    return new Ext.form.DateField(config);
                case "StringField":
                    config = {
                        id: value.id,
                        fieldLabel: localizedLabel || value.label,
                        name: value.name,
                        value: value.value
                    };
                    return new Ext.form.TextField(config);
                case "NumberField":
                    config = {
                        id: value.id,
                        fieldLabel: localizedLabel || value.label,
                        name: value.name,
                        value: value.value,
                        decimalPrecision: value.decimalPrecision
                    };
                    return new Ext.form.NumberField(config);
                case "BooleanField":
                    if (ctx && ctx.target == "propertySheet") {
                        var booleanStore = new Ext.data.ArrayStore({
                            fields: ['value'],
                            data : [
                                [true],
                                [false]
                            ]
                        });
                        return new Ext.form.ComboBox({
                            id: value.id,
                            name: value.name,
                            value: value.value,
                            valueField: 'value',
                            displayField: 'value',
                            editable: false,
                            fieldLabel: localizedLabel || value.label,
                            store: booleanStore,
                            typeAhead: false,
                            mode: 'local',
                            triggerAction: 'all',
                            emptyText:'Select a value...',
                            selectOnFocus:false,

                            getValue: function() {
                                return this.value == 'true';
                            }
                        });
                    } else {
                        config = {
                            id: value.id,
                            fieldLabel: localizedLabel || value.label,
                            name: value.name,
                            value: value.value,
                            checked: value.value
                        };
                        return new Ext.form.Checkbox(config);
                    }
                case "ArrayField":
                    if (value.value) {
                        value.value = value.value.join(","); // array -> csv string
                    } else {
                        value.value = "";
                    }

                    config = {
                        id: value.id,
                        fieldLabel: localizedLabel || value.label,
                        name: value.name,
                        value: value.value,
                        datastore: msStore
                    };
                    //var listEditField =  new MyDesktop.ListEditField(config);
                    var listEditField = Ext.create('MyDesktop.ListEditField', config);
                    return listEditField;
                // menu
                case "Item":
                    config = {
                        id: value.id,
                        text: localizedLabel || value.text
                    };
                    return new Ext.menu.Item(config);
                case "CheckItem":
                    config = {
                        id: value.id,
                        text: localizedLabel || value.text,
                        checked: value.checked
                    };
                    return new Ext.menu.CheckItem(config);
                default:
                    return new Ext.form.TextField(config);
            }
        }
    }

    return {
        transformServerMessageNode: function (value, ctx) {
            var retVal;
            if (isComponent(value)) {
                retVal = createAndDecorateComponent(value, ctx);
            } else
                retVal = value;
            return this._next_.transformServerMessageNode(retVal, ctx);
        },
        getNameValuePairFromField: function(field) {
            switch (field.getXType()) {
                case "multichoicefield":
                case "listeditfield":
                    var val = field.getValue();
                    var arr = [];
                    if (val != "") arr = val.split(",");
                    return [field.name, arr];
                case "datefield":
                    var date = field.getValue();
                    var dateAsString = (date.getMonth() + 1) + "/" + (date.getDate()) + "/" + (date.getYear() + 1900);
                    return [field.name, dateAsString];
                case "checkboxgroup":
                    var selectedCheckBoxes = field.getValue();
                    var selectedValues = [];
                    for (var i = 0; i < selectedCheckBoxes.length; i++) {
                        selectedValues[selectedValues.length] = selectedCheckBoxes[i].name;
                    }
                    return [field.name, selectedValues];
            }
            return this._next_.getNameValuePairFromField(field);
        }

    }
};

DefaultParacolExtJSMessageFilters = ParacolExtJSMessageFilters();